package com.xk857.article.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xk857.article.req.ArticleREQ;
import com.xk857.article.req.ArticleUserREQ;
import com.xk857.article.service.IArticleService;
import com.xk857.entities.Article;
import com.xk857.util.base.Result;
import com.xk857.util.enums.ArticleStatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 文章信息表 前端控制器
 *
 * @author CV大魔王
 * @since 2021-02-21
 */
@Api(value = "文章管理接口", tags = "文章管理接口")
@RestController
@RequestMapping("/pri/article")
public class ArticleController {

    @Autowired
    private IArticleService articleService;


    @ApiOperation("根据文章标题与状态查询文章分页列表接口")
    @PostMapping("/search")
    public IPage<Article> search(@RequestBody ArticleREQ req) {
        return articleService.queryPage(req);
    }


    @ApiOperation("查询文章详情接口")
    @ApiImplicitParam(name = "id", value = "文章ID", required = true)
    @GetMapping("/{id}")
    public Article view(@PathVariable("id") String id) {
        return articleService.findArticleAndLabelById(id);
    }


    @ApiOperation("修改文章信息接口")
    @PutMapping
    public Result update(@RequestBody Article article) {
        return Result.ok(articleService.updateOrSave(article));
    }


    @ApiOperation("新增文章信息接口")
    @PostMapping
    public Result save(@RequestBody Article article) {
        return Result.ok(articleService.updateOrSave(article));
    }


    @ApiImplicitParam(name = "id", value = "文章ID", required = true)
    @ApiOperation("删除文章接口")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) {
        // 假删除，通过文章 id 修改状态为 0 ，表示已删除
        articleService.updateStatus(id, ArticleStatusEnum.DELETE);
        return Result.ok();
    }


    @ApiImplicitParam(name = "id", value = "文章ID", required = true)
    @ApiOperation("审核通过接口")
    @GetMapping(value = "/audit/success/{id}")
    public Result success(@PathVariable("id") String id) {
        articleService.updateStatus(id, ArticleStatusEnum.SUCCESS);
        return Result.ok();
    }


    @ApiImplicitParam(name = "id", value = "文章ID", required = true)
    @ApiOperation("审核不通过接口")
    @GetMapping(value = "/audit/fail/{id}")
    public Result fail(@PathVariable("id") String id) {
        articleService.updateStatus(id, ArticleStatusEnum.FAIL);
        return Result.ok();
    }


    @ApiOperation("根据用户ID查询公开或未公开的文章列表")
    @PostMapping("/user")
    public IPage<Article> findListByUserId(@RequestBody ArticleUserREQ req) {
        return articleService.findListByUserId(req);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章ID", required = true),
            @ApiImplicitParam(name = "count", value = "点赞数", required = true)
    })
    @ApiOperation("更新点赞数")
    @PutMapping("/thumb/{id}/{count}")
    public Result updateThumhup(@PathVariable("id") String id, @PathVariable("count") int count) {
        return articleService.updateThumhup(id, count);
    }


    @ApiImplicitParam(name = "id", value = "文章ID", required = true)
    @ApiOperation("更新浏览次数")
    @PutMapping("/viewCount/{id}")
    public Result updateViewCount(@PathVariable("id") String id) {
        return articleService.updateViewCount(id);
    }


    @ApiOperation("统计审核通过且公开的文章总记录数")
    @GetMapping("/total")
    public Integer getArticleTotal() {
        return articleService.getArticleTotal();
    }


    @ApiOperation("统计各分类下的文章数")
    @GetMapping("/category/total")
    public Map<String, Object> categoryTotal() {
        return articleService.selectCategoryTotal();
    }


    @ApiOperation("查询近6个月发布的文章数")
    @GetMapping("/month/total")
    public Map<String, Object> monthArticleTotal() {
        return articleService.selectMonthArticleTotal();
    }


    @ApiOperation("查询今日文章数量")
    @GetMapping("/search/count/day/num")
    public Integer searchCountDayNum() {
        return articleService.searchCountDayNum();
    }

    @ApiOperation("查询昨日文章数量")
    @GetMapping("/search/count/yesterday/num")
    public Integer searchNumberOfArticlesInYesterdaySDay() {
        return articleService.searchNumberOfArticlesInYesterdaySDay();
    }

    @ApiOperation("查询近七天文章数量")
    @GetMapping("/search/count/week/num")
    public Integer searchNumberOfArticlesInWeek() {
        return articleService.searchNumberOfArticlesInWeek();
    }
}
